/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Connect;
import java.sql.*;
import java.util.Properties;


/**
 *
 * @author Mr-Nguyen
 */
public class ConnectionJDBC {
    
      
    private Connection cnn = null;
    private String ConnectionSTR;
    private String userName;
    private String passWord;

    public String getConnectionSTR() {
        return ConnectionSTR;
    }

    public String getPassWord() {
        return passWord;
    }

    public String getUserName() {
        return userName;
    }

    public void setConnectionSTR(String ConnectionSTR) {
        this.ConnectionSTR = ConnectionSTR;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public ConnectionJDBC() {
        
        this.ConnectionSTR = "jdbc:mysql://localhost:3306/zingdeal" ;
        this.userName = "root";
        this.passWord = "123456789";
    }

    public ConnectionJDBC(String ConnectionSTR, String userName, String passWord) {
        this.ConnectionSTR = ConnectionSTR;
        this.userName = userName;
        this.passWord = passWord;
    }
    public ConnectionJDBC(ConnectionJDBC cnn){
        this.ConnectionSTR = cnn.getConnectionSTR();
        this.passWord = cnn.getPassWord();
        this.userName= cnn.getUserName();
    }
    
    public Connection Connect(){
        try {
            Driver driver = new org.gjt.mm.mysql.Driver();
            DriverManager.registerDriver(driver);
            Properties info = new Properties();
            info.setProperty("user", this.getUserName());
            info.setProperty("password",this.getPassWord());
            info.setProperty("characterEnodeing","utf8");
            cnn = driver.connect(this.getConnectionSTR(), info);
            
        } catch (SQLException e) {
              System.out.print("\n -- Loi connect !" + e);
        }
        finally{
        return cnn;}
    }
    public void Close(){
        try {
            cnn.close();
        } catch (SQLException e) {
        }
    }
    public ResultSet getTable(String sql){
        ResultSet rs = null;
        try {
            Statement statement = cnn.createStatement();
            rs = statement.executeQuery(sql);
        } catch (SQLException e) {
            System.out.print("Loi getTable " + e);
        }
        finally{
            return rs;
        }
    }
    
    public int update(PreparedStatement preState){
        try {
            preState.execute();
           return preState.executeUpdate();
        } catch (SQLException e) {
            System.out.print("Loi update " + e);
            return 0;
        }
    }
    public int update(String sql){
        try {
            Statement state = cnn.createStatement();
            return state.executeUpdate(sql);
        } catch (SQLException e) {
            System.out.print("Loi update " + e);
            return 0;
        }
    }
    public int delete(String sql){
        try {
            Statement state = cnn.createStatement();
            state.executeQuery("set foreign_key_checks=0");
            int ok = state.executeUpdate(sql);
            state.executeQuery("set foreign_key_checks=0");
            return ok;
        } catch (Exception e) {
            return 0;
        }
    }
    public ResultSet getDataWithStoredProdueture(CallableStatement callState){
        ResultSet rs = null;
        try {
            callState.execute();
            rs = callState.getResultSet();
            return rs;
        } catch (Exception e) {
            return rs;
        }
    }

}
